new Vue({
    el: "#app",
    data: {
        fullscreenLoading: false,
        screenLoading: false,
        dialogFormVisible: false,
        adImgUrl: '',
        uploadAdUrl: '',
        productId: null,
        productName: '',
        adProductH5Url: '',
        adCateId:'',
        isUsed: 1,
        pageNo: 1,
        pageSize: 15,
        total: 0,
        pages: 0,
        queryContent: {
            keyword: ''
        },
        cateList:[],
        productData: [],
        tempId: '',
        tempName: '',
        onProcess: false,
        uploadPercent: 0,
        intervalId: null,
    },
    mounted: function () {
        var _this = this;
        _this.loadCateList();
    },
    methods: {
        trim: function (str) {
            if (isNaN(str)) {
                return str.replace(/(^\s*)|(\s*$)/g, "");
            } else {
                return str
            }
        },
        loadCateList:function () {
            var _this = this;
            axios.get(request_prefix + "/cash/adCate/list")
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.cateList = res.data.data;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        handleAvatarProcess: function (event, file, fileList) {
            var _this = this;
            _this.showStatus();
            _this.onProcess = true;
        },
        showStatus: function () {
            var _this = this;
            var intervalId = setInterval(function () {
                if (_this.uploadPercent < 100) {
                    _this.uploadPercent += 5;
                }
            }, 100);
        },
        handleAvatarSuccess: function (res, file) {
            var _this = this;
            _this.uploadAdUrl = res.data.pictureUrl;
            _this.adImgUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload: function (file) {
            var isJPG = file.type === 'image/jpeg';
            var isPNG = file.type === 'image/png';
            var isGIF = file.type === 'image/gif';
            var isLt1M = file.size / 1024 / 1024 < 1;

            if (!isJPG && !isPNG && !isGIF) {
                this.$message.error('上传图片格式错误!!');
            }

            if (!isLt1M) {
                this.$message.error('上传图片大小不能超过 1MB!');
            }

            if(this.adImgUrl != '') {
                this.adImgUrl = '';
            }
            this.uploadPercent = 0;
            this.onProcess = false;

            return (isJPG || isPNG || isGIF) && isLt1M;
        },
        selectProduct: function () {
            var _this = this;
            axios.get(request_prefix+"/cash/ad/productList", {
                params: {
                    currentPage: _this.pageNo,
                    keyword: _this.queryContent.keyword
                }
            })
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.productData = res.data.data.dataList;
                        if (_this.dialogFormVisible == false) {
                            _this.dialogFormVisible = true;
                        }
                    } else {
                        _this.$message.error("未能获取到数据");
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        handleCurrentChange: function () {
            var _this = this;
            axios.get(request_prefix+"/cash/ad/productList", {
                params: {
                    currentPage: _this.pageNo,
                    keyword: _this.queryContent.keyword
                }
            })
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.productData = res.data.data.dataList;
                    } else {
                        _this.$message.error("未能获取到数据");
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        resetQuery: function () {
            var _this = this;
            _this.queryContent.keyword = '';
            _this.pageNo = 1;
            _this.selectProduct();
        },
        getItemInfo: function (id, name) {
            var _this = this;
            _this.tempId = id;
            _this.tempName = name;
        },
        confirmGetInfo: function () {
            var _this = this;
            if (_this.tempId === '' || _this.tempName === '') {
                _this.$message.warning("请选择产品");
                return;
            }
            _this.productId = parseInt(_this.tempId);
            _this.productName = _this.tempName;
            _this.dialogFormVisible = false;
            _this.tempId = '';
            _this.tempName = '';
        },
        clearProduct: function () {
            var _this = this;
            _this.productName = '';
            _this.productId = null;
        },
        saveAdInfo: function () {
            var _this = this;
            if (_this.productId === null || _this.uploadAdUrl === '' || _this.adProductH5Url === '' || _this.isUsed == null) {
                _this.$message.warning("信息不完整，无法保存");
                return;
            }
            var isValidUrl = _this.validateURL(_this.adProductH5Url);
            if (!isValidUrl) {
                _this.$message.warning("广告链接不符合要求");
                return;
            }
            var data = new URLSearchParams();
            data.append("productId", _this.productId);
            data.append("adImgUrl", _this.uploadAdUrl);
            data.append("adProductH5Url", _this.adProductH5Url);
            data.append("isUsed", _this.isUsed);
            data.append("cateId",_this.adCateId);
            axios.post(request_prefix+"/cash/ad/create", data)
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.$message.success("创建成功，2秒后进入列表页");
                        setTimeout(function () {
                            window.location.href = request_prefix+"/cash/ad/listPage";
                        }, 2000);
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        toBackList: function () {
            window.location.href = request_prefix+"/cash/ad/listPage";
        },
        /* 合法uri*/
        validateURL: function (textval) {
            const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
            return urlregex.test(textval)
        }
    }
});